package treeStudy;

public class IsBalanced {

    /**
     * LeetCode T110 平衡二叉树
     * @param args
     */
    public static void main(String[] args) {

    }

    public boolean isBalanced(TreeNode root) {
        return dfs(root) == - 1 ? false : true;
    }
    public int dfs(TreeNode t){
        if (t == null){
            return 0;
        }

        int left = dfs(t.left);
        int right = dfs(t.right);
        if (left == -1 || right == -1 || Math.abs(left - right) > 1){
            return -1;
        }
        return 1 + Math.max(left, right);
    }
}
